package com.herench.bunch.emr.convert;

import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.group.ORU_R01_PATIENT_RESULT;
import ca.uhn.hl7v2.model.v24.message.ADT_A01;
import ca.uhn.hl7v2.model.v24.message.ORU_R01;
import ca.uhn.hl7v2.model.v24.message.QBP_Q11;
import ca.uhn.hl7v2.model.v24.message.QRY_Q01;
import ca.uhn.hl7v2.model.v24.segment.*;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class StatusChangesetConvert extends AbstractMessageConvert {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Map<String, Object> doToMessage(RequestMessage requestMessage) throws Exception {
        Map<String, Object> params = new HashMap<>();
        QRY_Q01 hl7Message = (QRY_Q01) requestMessage.getHl7Message();
        QRD qrd = hl7Message.getQRD();
        String patientId = qrd.getQrd4_QueryID().getValue();
        String startTime = qrd.getQrd1_QueryDateTime().getTs1_TimeOfAnEvent().getValue();
        String endTime = qrd.getQrd2_QueryFormatCode().getValue();
        params.put("patientId", patientId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        return params;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Message fromMessage(Object message) throws Exception {
        Assert.notNull(message, "message 为空.");
        List<Map<String, Object>> res = (List<Map<String, Object>> ) message;
        ADT_A01 adtA01 = new ADT_A01();
        adtA01.initQuickstart("ADT_A01", "ADT_A01", "A");
        MSH msh = adtA01.getMSH();
        initResponseMessageMSH(msh);
        PID pid =  adtA01.getPID();
        if(res.size()>0) {
            Map<String, Object> reqMap = res.get(0);

            pid.getPid2_PatientID().getCx1_ID().setValue(String.valueOf(reqMap.get("patientId")));
            pid.getPid1_SetIDPID().setValue(String.valueOf(reqMap.get("visitId")));

            for (int i = 0; i < res.size(); i++) {
                PR1 pr1 = adtA01.getPROCEDURE(i).getPR1();

                pr1.getPr12_ProcedureCodingMethod().setValue(String.valueOf(reqMap.get("action")));
                pr1.getPr13_ProcedureCode().getCe1_Identifier().setValue(String.valueOf(reqMap.get("logDateTime")));
                pr1.getPr13_ProcedureCode().getCe2_Text().setValue(String.valueOf(reqMap.get("wardCode")));
                pr1.getPr13_ProcedureCode().getCe3_NameOfCodingSystem().setValue(String.valueOf(reqMap.get("deptCode")));
                pr1.getPr13_ProcedureCode().getCe4_AlternateIdentifier().setValue(String.valueOf(reqMap.get("bedNo")));
                pr1.getPr13_ProcedureCode().getCe5_AlternateText().setValue(String.valueOf(reqMap.get("admissionDateTime")));
                pr1.getPr13_ProcedureCode().getCe6_NameOfAlternateCodingSystem().setValue(String.valueOf(reqMap.get("admWardDateTime")));
                pr1.getPr113_ConsentCode().getCe1_Identifier().setValue(String.valueOf(reqMap.get("diagnosis")));
                pr1.getPr113_ConsentCode().getCe2_Text().setValue(String.valueOf(reqMap.get("patientCondition")));
                pr1.getPr113_ConsentCode().getCe3_NameOfCodingSystem().setValue(String.valueOf(reqMap.get("nursingClass")));
                pr1.getPr113_ConsentCode().getCe4_AlternateIdentifier().setValue(String.valueOf(reqMap.get("operatingDate")));
                pr1.getPr113_ConsentCode().getCe5_AlternateText().setValue(String.valueOf(reqMap.get("bedLabel")));
            }
        }else {
            msh.getMsh15_AcceptAcknowledgmentType().setValue("无数据");
        }
        return adtA01;
    }

    @Override
    public Message generatorReqMessage() throws Exception {
        QBP_Q11 qbp = new QBP_Q11();
        qbp.initQuickstart("QBP_Q11", "Q11", "L");
        MSH msh = qbp.getMSH();
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("SSM");
        msh.getMessageControlID().setValue(IdGenerator.getUUID());
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("200140");
        msh.getMsh11_ProcessingID().getProcessingMode().setValue("QR");
        QPD qpd = qbp.getQPD();
        qpd.getMessageQueryName().getIdentifier().setValue("查询号别");
        qpd.getQpd2_QueryTag().setValue("QR");
        return qbp;
    }
}
